﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using PracticalNEU.Models;
using PracticalNEU.Services;
using Xamarin.Forms;

namespace PracticalNEU.ViewModels
{
    public class TodayWeatherPageViewModel : ViewModelBase
    {
        /// <summary>
        /// 今日天气。
        /// </summary>
        private TodayWeather _weather;


        public List<WeatherDataItem> DataItem { get; set; }

        /// <summary>
        /// 今日天气。
        /// </summary>
        public TodayWeather Weather
        {
            get => _weather;
            set => Set(nameof(Weather), ref _weather, value);
        }

        /// <summary>
        /// 今日天气接口。
        /// </summary>
        private ITodayWeatherStorage _todayWeatherStorage;

        /// <summary>
        /// 构造方法。
        /// </summary>
        /// <param name="todayWeatherStorage"></param>
        public TodayWeatherPageViewModel(ITodayWeatherStorage todayWeatherStorage)
        {
            _todayWeatherStorage = todayWeatherStorage;
        }

        /// <summary>
        /// 页面显示命令。
        /// </summary>
        public RelayCommand _pageAppearingCommand;

        /// <summary>
        /// 页面显示命令。
        /// </summary>
        public RelayCommand PageAppearingCommand =>
            _pageAppearingCommand ?? (_pageAppearingCommand =
                new RelayCommand(async () => await PageAppearingCommandFunction()));
        /// <summary>
        /// 页面访问方法。
        /// </summary>
        /// <returns></returns>
        public async Task PageAppearingCommandFunction()
        {
            //await _todayWeatherStorage.InitializeAsync();

            var todayWeather = await _todayWeatherStorage.GetTodayWeatherFromDataBaseAsync();
            var date = DateTime.Now.ToString("yyyy-MM-dd");
            if (!(todayWeather.Date.Equals(date)))
            {
                await _todayWeatherStorage.GetTodayWeatherFromApiAsync();
                todayWeather = await _todayWeatherStorage.GetTodayWeatherFromDataBaseAsync();
            }
            Weather = todayWeather;
            if (DataItem == null)
            {
                DataItem = new List<WeatherDataItem>();
                DataItem.Add(new WeatherDataItem
                {
                    Name = "最高气温",
                    Value = Weather.MaxTemp.ToString()
                });
                DataItem.Add(new WeatherDataItem
                {
                    Name = "最低气温",
                    Value = Weather.MinTemp.ToString()
                });
                DataItem.Add(new WeatherDataItem
                {
                    Name = "风向",
                    Value = Weather.Win
                });
                DataItem.Add(new WeatherDataItem
                {
                    Name = "风速",
                    Value = Weather.WinSpeed
                });
                DataItem.Add(new WeatherDataItem
                {
                    Name = "空气等级",
                    Value = Weather.AirLevel
                });
                DataItem.Add(new WeatherDataItem
                {
                    Name = "空气指数",
                    Value = Weather.Air.ToString()
                });
                DataItem.Add(new WeatherDataItem
                {
                    Name = "PM2.5指数",
                    Value = Weather.AirPm.ToString()
                });
                DataItem.Add(new WeatherDataItem
                {
                    Name = "空气指数",
                    Value = Weather.Air.ToString()
                });
            }
            

        }
    }
}
